// pages/ai/ai.js

let db = wx.cloud.database()
let app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        question: '',
        chats: 0,
        dialog: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                active: 2
            })
        }
        this.setData({windowHeight: wx.getSystemInfoSync()['windowHeight']})
        db.collection("userDB").where({
          _openid : app.globalData.openid
        }).get({
          success:(res) =>{
            console.log(res);
            this.setData({
              avatar:res.data[0].avatar
            })
          }
        })
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    send(){
        let that = this
        let dialog = this.data.dialog
        let question = this.data.question
        if (question.length == 0) {
            question = '推荐几个类似UNO的桌游'
        }
        dialog.push({'role':'user','content':{'question': question}})
        dialog.push({'role':'ai','content':''})
        
        this.setData({dialog,question: ''})
        wx.request({
            url: 'https://www.drawu.com.cn:25565/ai', //仅为示例，并非真实的接口地址
            method: 'POST',
            header: {
              'content-type': 'application/json' // 默认值
            },
            data: {
                question: question
            },
            success (res) {
                dialog.pop()
                console.log(res.data);
                let type = res.data.type
                if (type == "recommendation") {
                  let recommendation = res.data.recommendation
                  let bgArr = recommendation.split(',')
                  db.collection('bgDB').where({
                    name: db.command.or(bgArr)
                  }).get({
                    success: (res) => {
                      console.log(res.data);
                      let obj = {
                        type: "recommendation",
                        bgArr: res.data
                      }
                      dialog.push({'role':'ai','content': {type: "detail",detail: "您可以看看下面这三个桌游^v^"}})
                      dialog.push({'role':'ai','content': obj})
                      console.log(dialog);
                      that.setData({dialog})
                    }
                  })
                } else if (type == "video"){
                  let obj = {
                    type: "video",
                    video: res.data.video
                  }
                  dialog.push({'role':'ai','content': {type: "detail",detail: "您可以看看这个视频哦，里面有简单的教学^v^"}})
                  dialog.push({'role':'ai','content': obj})
                  that.setData({dialog})
                } else if (type == "detail") {
                  let obj = {
                    type: "detail",
                    detail: res.data.detail
                  }
    
                  dialog.push({'role':'ai','content': obj})
                  that.setData({dialog})
                } else {
                  let obj = {
                    type: "other",
                    answer: res.data.answer
                  }
                  dialog.push({'role':'ai','content': obj})
                  that.setData({dialog})

                }

            }
          })
    },
    gotoBoardGame(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../BoardGame/BoardGame',
            success: function(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage',{id: id})
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})